// pages/getExpress/getExpress.js
import logFunction from "../../utils/Log";
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [{
        name: '小件',
        tips: '小件快递是指在1公斤以内的物品，收费1元'
      },
      {
        name: '中件',
        tips: '中件快递是指鞋盒大小的物品，收费3元'
      },
      {
        name: '大件',
        tips: '快递重量在3-30公斤左右的物品，收费5元'
      }
    ],
    typenow: 0,
    showMore: false,
    isReward: false,
    businessIndex: 0,
    businessArray: ['韵达快递', '圆通速递', '申通快递', '中通快递', '顺丰快递'],
    arriveIndex: 0,
    arriveArray: ['尽快送达', '中午', '晚上', '不限制时间'],
    sexIndex: 0,
    sexArray: ['不限制性别', '男', '女'],
    numIndex: 0,
    numArray: ['1', '2-3个', '3个以上'],
    selectBusiness: false,
    checked: "不限制性别"
  },
  getCurrentTime() {
    var month = new Date();
    // console.log(month.getHours()+":"+month.getMinutes());
    let min = (month.getMinutes() - 15) + '';
    console.log(min);
    if (min.length == 1) {
      min = '0' + min
    }
    let currentTime = month.getHours() + ":" + min;
    console.log(currentTime);
    this.setData({
      currentTime,
    })
    return currentTime;
  },
  onSubmit() {
    logFunction.logFunction.getUserProfile(this);

    this.getCurrentTime();
    // console.log(this.__data__.getLocation);
    // if (!getApp().globalData.userInfo) {
    //   logFunction.logFunction.getUserProfile(this);

    // } else {
      if (getApp().globalData.judgeUser) {
        
      db.collection("orders").add({
        data: {
          pakingLocation: '快递超市',
          getLocation: this.__data__.myLocation,
          // time: this.__data__.time,
          currentTime: this.__data__.currentTime,
          thing: this.__data__.thing,
          priNum: this.__data__.priNum,
          // ps: this.__data__.ps,
          type: "快递代取",
          thingImg: this.__data__.thingImg,
          checked: this.__data__.checked,
          // userInfo.avatarUrl
          avatarUrl: getApp().globalData.userInfo.avatarUrl,
          nickName: getApp().globalData.userInfo.nickName,

        },
        success: (result) => {
          // console.log(result);
          // Toast('SUCCESS!');
          console.log("success");
          setTimeout(() => {
            wx.navigateBack();
          }, 1000);

        },
        fail: (res) => {
          console.log(res);
        },
        complete: (res) => {

        }
      })
    }
  },

  // //判断用户是否已经授权
  // async judgeUser() {
  //   let judge = (await db.collection("userInfo").get()).data.length;
  //   console.log((await db.collection("userInfo").get()).data);
  //   //   db.collection("userInfo").get().then(() => {
  //   //     if (judge) {
  //   //         this.setData({
  //   //     judgeUser: true,
  //   //   })
  //   // }
  //   // })
  //   if (judge) {
  //     this.setData({
  //       judgeUser: true,
  //     })
  //   }
  //   console.log(333);
  //   return new Promise((resolve, reject) => {
  //     resolve('222')
  //   })
  // },

  //上传截图
  upload() {

    //     console.log(this.judgeUser(this.__data__.judgeUser));
    // this.judgeUser().then((res) => {
    //   console.log(1000);
    //   if (!this.__data__.judgeUser) {
    //     logFunction.logFunction.getUserProfile(this);
    //   }
    //   console.log(res);
    // }, (res) => {
    //   console.log(res);
    // })

    // console.log(1001);

    // this.judgeUser();
    // console.log(1234567);
    // console.log(this.__data__.judgeUser);

    // logFunction.logFunction.judgeUser(this);
    //     wx.setTimeout({


    //     },1000)
    // if (!this.__data__.judgeUser) {
      logFunction.logFunction.getUserProfile(this);
    // } else {
      if (getApp().globalData.judgeUser) {
         wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          wx.showLoading({
            title: '加载中',
          })
          const random = Math.floor(Math.random() * 1000);
          wx.cloud.uploadFile({
            cloudPath: `avatar/${this.data.userInfo.nickName}-${random}.png`,
            filePath: res.tempFilePaths[0],
            success: (res) => {
              let fileID = res.fileID;
              // userInfo.avatarUrl = fileID;
              this.setData({
                userInfo,
              })
              wx.hideLoading();
              console.log(res);
              wx.cloud.downloadFile({
                fileID: res.fileID, // 文件 ID
                success: res => {
                  // 返回临时文件路径
                  console.log(res.tempFilePath)
                  this.setData({
                    thingImg: res.tempFilePath
                  })
                },
                fail: console.error
              })
            },
            fail: (res) => console.log(res)
          })
        }
      })
      }
      // let userInfo = this.data.userInfo;
      // console.log(userInfo);
     
    // }

  },
  bindBusinessChange(e) {
    this.setData({
      businessIndex: e.detail.value,
      selectBusiness: true
    })
  },

  bindArriveChange(e) {
    this.setData({
      arriveIndex: e.detail.value,
      selectBusiness: true
    })
  },

  bindSexChange(e) {
    this.setData({
      sexIndex: e.detail.value,
      selectBusiness: true
    })
  },

  bindNumChange(e) {
    this.setData({
      numIndex: e.detail.value,
      selectBusiness: true
    })
  },

  handleChangeReward(e) {
    const value = e.detail.value;
    this.setData({
      isReward: value
    })
  },

  showMore() {
    this.setData({
      showMore: !this.data.showMore
    })
  },
  selectType(e) {
    const {
      id,
      tip
    } = e.currentTarget.dataset;
    this.setData({
      typenow: id,
    });

    if (this.__data__.typenow === 0) {
      this.setData({
        priNum: 1,
        thing: '小件快递'
      });
    }
    if (this.__data__.typenow === 1) {
      this.setData({
        priNum: 3,
        thing: '中件快递',
      })
    }
    if (this.__data__.typenow === 2) {
      this.setData({
        priNum: 5,
        thing: '大件快递'
      });
    }
    this.setData({
      thing: '小件快递'
    })

    console.log(this.__data__.priNum);
    wx.showToast({
      icon: 'none',
      title: tip,
    })
  },

  selectAddress() {
    wx.navigateTo({
      url: '../address/address',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    

    // this.judgeUser();
    logFunction.logFunction.judgeUser(this);
    this.setData({
      priNum: 1,
      thing: '小件快递',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})